package com.kma.summer2012.client.view.requests;

import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.kma.summer2012.client.constants.ConstantsHolder;
import com.kma.summer2012.shared.model.UICrumbsDateAndTime;
import com.kma.summer2012.shared.model.UICrumbsRequest;
import com.kma.summer2012.shared.model.UICrumbsToolItem;
import com.kma.summer2012.shared.model.UICrumbsWorkItem;

import java.util.ArrayList;
import java.util.Date;

public class
        ShowManager extends Request {
    @UiTemplate("ShowRequest.ui.xml")
    interface ShowManagerUiBinder extends UiBinder<HTMLPanel, Request> {
    }

    private static ShowManagerUiBinder showRequestUibinder = GWT.create(ShowManagerUiBinder.class);

    public ShowManager() {
        super(showRequestUibinder);

        this.title.setText(constants.showRequest());
        this.saveButton.setEnabled(false);
        this.deleteCloseButton.setText(constants.delete());
        this.editButtonManager.setVisible(false);
        this.editButtonOperator.setVisible(false);
        this.sendForProcessingButton.setVisible(false);
        this.deleteCloseButton.setVisible(false);
        this.cancelButton.setVisible(false);
        this.rejectButton.setVisible(false);
        this.closeButton.setVisible(false);
        this.backButton.setVisible(true);
        this.exportButton.setVisible(false);
        enableField();
        this.labelComments.setText("");
        this.comments.setVisible(false);

    }

    @Override
    public void setParametres(UICrumbsRequest request) {
        id.setText("" + request.getId());
        datelbl.setText(request.getDateOfCreation().toString());
        state.setText(request.getState());
        clAddress.setText(request.getUicrumbsClient().getAdress());
        clMail.setText(request.getUicrumbsClient().getMail());
        clName.setText(request.getUicrumbsClient().getName());

        if (request.getUicrumbsWorkItems().size() > 0) {
            for (UICrumbsWorkItem key : request.getUicrumbsWorkItems().keySet()) {
                ArrayList<String> tmp = new ArrayList<String>();
                amountsOfWorkValues.put(key.getId(), (int) request.getUicrumbsWorkItems().get(key));
                tmp.add(key.getWorkName());
                tmp.add("" + key.getId());
                Integer k = new Integer(key.getId());
                workList.put(k, key);
                TextBox txt = new TextBox();
                txt.setValue("" + request.getUicrumbsWorkItems().get(key));
                amountOfWorkInTxtBox.put(k, txt);
                this.totalSumOfWork += key.getRuntime() * request.getUicrumbsWorkItems().get(key);

            }
            addeWorkField();
        } else {
            totalSumOfWork = 0;
        }
        if (request.getRequestInfo() != null)
            requestInfo.setText(request.getRequestInfo());
        else
            requestInfo.setText("");

        //timeRequired.setText("" +totalSumOfWork);

        if (request.getUicrumbsDateAndTimes().size() > 0) {
            int index = 0;
            int id = 1;
            for (UICrumbsDateAndTime datatime : request.getUicrumbsDateAndTimes()) {
                if (index == 0) {
                    for (int i = 9; i < 19; i++) {
                        Date timestart = datatime.getDateBegin();
                        Date timeend = datatime.getDateEnd();
                        String st = DateTimeFormat.getFormat(ConstantsHolder.getConstants().DATE_FORMAT_FULL()).format(timestart);
                        String end = DateTimeFormat.getFormat(ConstantsHolder.getConstants().DATE_FORMAT_FULL()).format(timeend);
                        int s = DateTimeFormat.getFormat(ConstantsHolder.getConstants().DATE_FORMAT_FULL()).parse(st).getHours();
                        int e = DateTimeFormat.getFormat(ConstantsHolder.getConstants().DATE_FORMAT_FULL()).parse(end).getHours();
                        if (i == s) {

                            startTimeList.setSelectedIndex(id);
                            startTimeList.setEnabled(false);
                            start_time = startTimeList.getItemText(id);
                            id++;
                        } else if (i == e) {

                            endTimeList.setSelectedIndex(id);
                            endTimeList.setEnabled(false);
                            end_time = endTimeList.getItemText(id);
                            id++;
                        } else {
                            id++;
                        }

                    }
                    String dateString = DateTimeFormat.getFormat(ConstantsHolder.getConstants().DATE_FORMAT_SHORT()).format(datatime.getDateBegin());
                    text.setText(dateString);
                    try {
                        this.timeAssigned.setText(DateTimeFormat.getFormat(ConstantsHolder.getConstants().DATE_FORMAT_FULL()).format(datatime.getAssignedTimeBegin()).concat(" - " + DateTimeFormat.getFormat(ConstantsHolder.getConstants().DATE_FORMAT_FULL()).format(datatime.getAssignedTimeEnd())));
                        this.labelTimeAssigned.setText(constants.assignedTime());
                        timeRequired.setText("");
                        labelTimeRequired.setText("");
                    } catch (NullPointerException e) {

                        timeAssigned.setText("No assigned time");
                    }
                    try {
                        if (request.getEngineer() != null) {
                            this.labelAssignedEngineer.setText(constants.ASSIGNED_ENGINEER());
                            if (request.getEngineer().getPhone() != null)
                                this.AssignedEngineer.setText(request.getEngineer().getSurnmNmSecondnm().concat(" (" + request.getEngineer().getPhone() + ")"));
                            else this.AssignedEngineer.setText(request.getEngineer().getSurnmNmSecondnm());
                        }
                    } catch (NullPointerException w) {
                    }

                } else break;
            }
            try {
                if (request.getUicrumbsToolsItems() != null) {
                    this.labelAdditionalTools.setText(constants.ADDITIONAL_TOOLS());

                    for (UICrumbsToolItem tool : request.getUicrumbsToolsItems().keySet()) {

                        additionalTools.add(new Label(tool.getToolName().concat("(" + request.getUicrumbsToolsItems().get(tool) + ")")));
                    }
                }
            } catch (NullPointerException w) {
            }
            //   request.
            //  this.startTimeList.setSelectedIndex();
        }

    }

    public void addeWorkField() {

        buttonForCatalog.add(this.catalog);

        int numRows = flexTable.getRowCount();
        int i = 0;
        for (int key : workList.keySet()) {


            flexTable.setWidget(i, 0, new Label(workList.get(key).getWorkName()));
            try {
                flexTable.setWidget(i, 1, new Label("" + amountsOfWorkValues.get(key)));
            } catch (NumberFormatException e) {

                flexTable.setWidget(i, 1, new Label("" + 1));
            }
            i++;
        }

        flexTable.getFlexCellFormatter().setRowSpan(0, 1, numRows + 1);
    }


    @Override
    public Widget asWidget() {
        return this;
    }
}


